package com.hr.service.impl;

import com.hr.domain.dto.UserFavoriteDTO;
import com.hr.domain.po.Favorite;
import com.hr.domain.vo.FavoriteVO;
import com.hr.domain.vo.UserFavoriteVO;
import com.hr.service.FavoriteService;
import com.hr.mapper.FavoriteMapper;
import com.hr.utils.UserContext;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 86150
 * @description 针对表【favorite(用户收藏表)】的数据库操作Service实现
 * @createDate 2025-11-11 10:48:12
 */
@Service
@AllArgsConstructor
public class FavoriteServiceImpl implements FavoriteService{

    private final FavoriteMapper favoriteMapper;

    /**
     * 添加收藏
     * @param houseId 房源ID
     * @param userId 用户ID
     * @return 是否添加成功
     */
    @Override
    public boolean addFavorite(Long houseId, Long userId) {
        // 先检查是否已收藏
        Favorite existingFavorite = favoriteMapper.findByUserIdAndHouseId(userId, houseId);
        if (existingFavorite != null) {
            // 已收藏，直接返回成功
            return true;
        }
        // 添加收藏
        int row = favoriteMapper.addFavorite(houseId, userId);
        return row > 0;
    }

    /**
     * 取消收藏
     * @param houseId 房源ID
     * @param userId 用户ID
     * @return 是否取消成功
     */
    @Override
    public boolean cancelFavorite(Long houseId, Long userId) {
        int row = favoriteMapper.cancelFavorite(houseId, userId);
        return row > 0;
    }

    /**
     * 获取收藏列表
     * @return 收藏列表
     */
    @Override
    public List<FavoriteVO> getFavoriteList() {
        List<FavoriteVO> houseIdList = favoriteMapper.selectFavoriteList();

        if (houseIdList != null){
            return houseIdList;
        }
        return null;
    }

    /**
     * 获取单个用户收藏列表
     * @return 收藏列表
     */
    @Override
    public List<UserFavoriteVO> getOneUserFavoriteList(UserFavoriteDTO userFavoriteDTO ) {
        // 获取当前登录用户ID
        Long userId = UserContext.getUserId();
        /*Long userId = 1L;*/
        userFavoriteDTO.setUserId(userId);
        List<UserFavoriteVO> houseIdList = favoriteMapper.selectByUserId(userFavoriteDTO);

        if (houseIdList != null){
            return houseIdList;
        }
        return null;

    }


}




